/*
 * KIMWONSORK@GMAIL.COM
 * COPYRIGHT CRAYONSOFT All rights reserved.
 * 2013오후 8:51:08
 * kws
 */

package com.crayon.account.item.adapter;


import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;

import com.crayon.account.dao.DAOAccount;
import com.crayon.account.data.ItemGroup;
import com.crayon.sharemenu.R;
import com.crayon.util.GDB;
import com.crayon.util.UIBIND;
import com.crayon.util.UTILCurrency;
import com.crayon.util.UTILDate;
import com.crayon.util.UTILSC;

import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;


public class AdapterAccountExpandable extends SimpleExpandableListAdapter {

	public static final String NAME_GROUP="key";
	private LayoutInflater mInflater = null;
	public AdapterAccountExpandable(Context context,
			List<? extends Map<String, ?>> groupData, int groupLayout,
					String[] groupFrom, int[] groupTo,
					List<? extends List<? extends Map<String, DAOAccount>>> childData,
							int childLayout, String[] childFrom, int[] childTo) {
		super(context, groupData, groupLayout, groupFrom, groupTo, childData,childLayout, childFrom, childTo);
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}


	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = convertView!=null?convertView:mInflater.inflate(R.layout.adapter_group_list, null);
		if(groupPosition >(getGroupCount()+1)) return view;
		Object object = getGroup(groupPosition);
		Map<String, ItemGroup> map = (Map)getGroup(groupPosition);
		ItemGroup itemAccountUI =  map.get(NAME_GROUP);
		UIBIND.bindText(view, R.id.date, itemAccountUI.getKey());
		if(itemAccountUI.getDate()!=null){
			UIBIND.bindText(view, R.id.dateWeek, UTILDate.getDayofWeeksToString(itemAccountUI.getDate()));
		}else{
			UIBIND.bindView(view, R.id.dateWeek).setVisibility(View.GONE);
		}
		UIBIND.bindText(view, R.id.textChildCount,String.format("%d",  itemAccountUI.getChildCount()));
		
		//중간 토탈과 인 아웃을 표시한다.
		UIBIND.bindText(view, R.id.textMidTop,String.format("%s",UTILCurrency.formatWonPrice(  itemAccountUI.getPriceIncom() - itemAccountUI.getPriceOutcom())));
		UIBIND.bindText(view, R.id.textMidCenter,String.format("%s",  UTILCurrency.formatWonPrice(itemAccountUI.getPriceIncom())));
		UIBIND.bindText(view, R.id.textMidRight,String.format("%s",  UTILCurrency.formatWonPrice(itemAccountUI.getPriceOutcom())));
		return view;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Map<String, ItemGroup> map = (Map)getGroup(groupPosition);
		ItemGroup itemGroup = map.get(NAME_GROUP);
		Map<String,DAOAccount> mapChild = (Map)getChild(groupPosition, childPosition);
		DAOAccount item = mapChild.get(itemGroup.getKey());
		if(item==null) return null;
		View layout =null;
//		if(UTILSC.isINCOMECheckIOType(item)){
//		layout   =
////				convertView!=null ? convertView:
//					mInflater.inflate(R.layout.adapter_child_account_month_list_in, null);
////			UIBIND.bindImageView(layout, R.id.imageIOType, android.R.drawable.arrow_up_float);
//		}else if(item.getxIOType()==EDAType.OUTCOM.getKey()){//out com...
//			layout   = 
////					convertView!=null ? convertView:
//						mInflater.inflate(R.layout.adapter_child_account_month_list_out, null);
////			UIBIND.bindImageView(layout, R.id.imageIOType, android.R.drawable.arrow_down_float);
////
//		}else{
			layout   = 
//					convertView!=null ? convertView:
						mInflater.inflate(R.layout.adapter_child_account_month_list, null);
//			UIBIND.bindImageView(layout, R.id.imageIOType, android.R.drawable.ic_dialog_info);

//		}
		if(isLastChild){
			UIBIND.bindViewBackgroundResource(layout, R.id.layoutExChildContents, R.drawable.shape_round_exgroup_bg_green_bottom);
			UIBIND.bindViewVisible(layout, R.id.layoutChildLastMargin, true);
		}else{
			UIBIND.bindViewBackgroundResource(layout, R.id.layoutExChildContents, R.drawable.shape_round_exgroup_bg_green);
			UIBIND.bindViewVisible(layout, R.id.layoutChildLastMargin, false);
		}
		TextView textSummary = UIBIND.bindText(layout, R.id.textSummary, String.format("%s", item.getSummary()));
//		textSummary.setTextSize((float)(textSummary.getTextSize()*(1.5)));
		
		UIBIND.bindText(layout, R.id.textYYYMMDD, UTILDate.getYYYYMMDDwithPeriod(item.getDtStart()));

		
		TextView textPrice = UIBIND.bindText(layout, R.id.textPrice, UTILCurrency.formatWonPrice(item.getXprice()));
//		textPrice.setTextSize((float)(textPrice.getTextSize()*(1.5)));
		
		if(UTILSC.isINCOMECheckIOType(item)){
			textPrice.setTextColor(Color.BLUE);
		}else{
			textPrice.setTextColor(Color.RED);
		}

		
		UIBIND.bindText(layout, R.id.textCate, !StringUtils.isEmpty(item.getxCategory()) ? item.getxCategory():
			layout.getContext().getString(R.string.dao_key_nocate));
		
		UIBIND.bindText(layout, R.id.textMemo,!StringUtils.isEmpty(item.getDesciption())  ? item.getDesciption():
			layout.getContext().getString(R.string.dao_no_datas));
		UIBIND.bindText(layout, R.id.textAccountName, !StringUtils.isEmpty(item.getXaccountName())?item.getXaccountName():"미분류");
		
		
		//
		if(item.getxImage()!=null){
			try {
				UIBIND.bindImageViewBitmapArray(layout, R.id.imageViewPhoto, item.getxImage());
				
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		return layout;
	}


	private String groupKeyName(int groupPosition) {
		Map<String, ItemGroup> map = (Map) getGroup(groupPosition);
		ItemGroup itemGroup = map.get(NAME_GROUP);
		if (itemGroup != null) {
			return itemGroup.getKey();
		}
		return null;
	}
	
	public DAOAccount getSelectedChildCalendar(int groupPosition, int childPosition) {
		String groupKey = groupKeyName(groupPosition);
		Map<String, DAOAccount> map = (Map) getChild(groupPosition, childPosition);
		
		GDB.log(getClass(), "groupKey===" + groupKey);
		GDB.log(getClass(), "SIZE===" + map.size());
		return (DAOAccount) map.get(groupKey);
	}


}
